package com.cqs.leetcode;

public class IslandPerimeter463 {


    /**
     * 到的格子 和
     *
     * @param grid
     * @return
     */
    public int islandPerimeter(int[][] grid) {
        int sum = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 0) continue;
                //四个方向是否和水相邻
                sum = i - 1 < 0 ? sum + 1 : sum + 1 - grid[i - 1][j];
                sum = i + 1 >= grid.length ? sum + 1 : sum + 1 - grid[i + 1][j];
                sum = j - 1 < 0 ? sum + 1 : sum + 1 - grid[i][j - 1];
                sum = j + 1 >= grid[0].length ? sum + 1 : sum + 1 - grid[i][j + 1];
            }
        }
        return sum;
    }


    public static void main(String[] args) {
        int[][] grid = new int[4][4];
        grid[0] = new int[]{0, 1, 0, 0};
        grid[1] = new int[]{1, 1, 1, 0};
        grid[2] = new int[]{0, 1, 0, 0};
        grid[3] = new int[]{1, 1, 0, 0};


        IslandPerimeter463 perimeter463 = new IslandPerimeter463();
        int result = perimeter463.islandPerimeter(grid);
        System.out.println(result);

    }
}
